package guava;

import com.google.common.base.*;
import com.google.common.collect.Collections2;
import org.junit.Test;
import org.xukai.common.ComparableStudent;

import java.util.*;
import java.util.concurrent.TimeUnit;

import static org.junit.Assert.assertEquals;

/**
 * @author xukai
 * @desc
 * @date 2016-11-29-上午 8:28
 */
public class FunctionUtil {

    @Test
    public void forMap(){
        Map<String, ComparableStudent> states = new HashMap<String, ComparableStudent>();
        states.put("12",new ComparableStudent("xukia",99,11));
        Function<String, ComparableStudent> lookup = Functions.forMap(states);
        //你也可以给不存在的key指定一个默认值
//        Function<String, Student> lookup = Functions.forMap(states, null);
        System.out.println(lookup.apply("12"));//key不存在会抛异常
        TemperateClimatePredicate predicate = new TemperateClimatePredicate();
        boolean apply = predicate.apply(new City());

    }

    public class City {
        private String name;
        private String zipCode;
        private int population;

        @Override
        public String toString() {
            return name;
        }
    }

    /**
     * 选择气候为TEMPERATE的城市
     */
    public class TemperateClimatePredicate implements Predicate<City> {
        @Override
        public boolean apply(City input) {
            return true;
        }
    }

    /**
     * 选择雨量小于45.7的城市
     */
    public class LowRainfallPredicate implements Predicate<City> {
        @Override
        public boolean apply(City input) {
            return false;
        }
    }

    /**
     * Supplier
     */
    @Test
    public void SupplierTest() throws InterruptedException {
        Supplier<ComparableStudent> sc = new Supplier<ComparableStudent>() {
            @Override
            public ComparableStudent get() {
                return new ComparableStudent();
            }
        };

        System.out.println();
        Supplier<ComparableStudent> memoize = Suppliers.memoize(sc);
        System.out.println(Suppliers.memoize(sc));//返回同一对象, 单例
        System.out.println(memoize.get().hashCode());
        System.out.println(memoize.get().hashCode());
        System.out.println(memoize.get().hashCode());
        Supplier<ComparableStudent> supplier = Suppliers.memoizeWithExpiration(sc, 5, TimeUnit.SECONDS);
        ComparableStudent c = supplier.get();
        System.out.println(c.hashCode());
        Thread.sleep(3000);
        c = supplier.get();
        System.out.println(c.hashCode()); //与之前相等
        Thread.sleep(3000);
        c = supplier.get();
        System.out.println(c.hashCode()); //与之前不等
    }

    @Test
    public void tes() throws InterruptedException {
        List<String> list = Arrays.asList("徐开1", "徐开2", "徐开3", "徐开4");
        final List<String> depts = Arrays.asList("徐开1");
        Collection<String> filter = Collections2.filter(list, new Predicate<String>() {
            @Override
            public boolean apply(String input) {
                return depts.contains(input);
            }
        });
        list = new ArrayList<>(filter);
        System.out.println(list.size());
    }




}
